<template>

  <div style="width: 40%;margin-top:5%">
    <br>
    <br>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="群组号" prop="GroupId">
        <el-input v-model="ruleForm.GroupId"></el-input>
      </el-form-item>
      <el-form-item label="群组名" prop="GroupName">
        <el-input v-model="ruleForm.GroupName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addTab('ruleForm')">创建群组</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        ps: this.$store.state.permission,
        AdminId:this.$route.query.AdminId,
        ruleForm: {
          GroupId: '',
          GroupName: '',
        },
        rules: {
          GroupName: [{
              required: true,
              message: '请输入组名',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 10,
              message: '长度在 2 到 10 个字符',
              trigger: 'blur'
            }
          ],
          GroupId: [{
            required: true,
            message: '请填写群组号',
            trigger: 'blur'
          }],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      addTab(formName) {
        this.FormVisible = false;
        this.$refs[formName].validate((valid) => {
          if (valid) {
              let user = JSON.stringify(this.ruleForm);
              console.log(user);
              this.$axios
                .get('/api/Group/CreateGroup', {
                  params:{
                    GroupId:this.ruleForm.GroupId,
                    GroupName:this.ruleForm.GroupName,
                    AdminId:this.AdminId,
                  },
                })
                .then(res => {
                  if (res.data.code == "200") {
                    this.$message({
                      type: "success",
                      message: "添加成功"
                    });
                    this.$router.push({
                      path: '/manage/addmemberinfo',
                      query: {
                        AdminId: this.$store.state.userId,
                        GroupId:this.ruleForm.GroupId,
                      }
                    });
                  }
                  if (res.data.code == "500") {
                    this.$message({
                      type: "error",
                      message: "添加失败"
                    });
                  }
                })
                .catch(err => {
                  console.log(err);
                });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    }
  }
</script>

<style>
</style>
